package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.LendStatusEnum;
import com.atguigu.srb.core.mapper.LendMapper;
import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.pojo.entity.Lend;
import com.atguigu.srb.core.pojo.vo.BorrowInfoApprovalVO;
import com.atguigu.srb.core.service.DictService;
import com.atguigu.srb.core.service.LendService;
import com.atguigu.srb.core.util.LendNoUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 标的准备表 服务实现类
 * </p>
 *
 * @author lupengning
 * @since 2021-05-27
 */
@Service
public class LendServiceImpl extends ServiceImpl<LendMapper, Lend> implements LendService {

  @Resource
  private DictService dictService;

  @Transactional(rollbackFor = Exception.class)
  @Override
  public void createLend(BorrowInfoApprovalVO borrowInfoApprovalVO, BorrowInfo borrowInfo) {

    // 起利息日期
    LocalDate lendStartDate = LocalDate
        .parse(borrowInfoApprovalVO.getLendStartDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    // 结束利息日期
    LocalDate lendEndDate = lendStartDate.plusMonths(borrowInfo.getPeriod());

    /*年化率*/
    BigDecimal lendYearRate = borrowInfoApprovalVO.getLendYearRate().divide(new BigDecimal(100));

    /*平台服务费率*/
    BigDecimal serviceRate = borrowInfoApprovalVO.getServiceRate().divide(new BigDecimal(100));

    // 平台预期收益 = (年化 / 12 ) * 标的金额 * 期数
    BigDecimal monthRate = serviceRate.divide(new BigDecimal(12), 8, RoundingMode.DOWN);
    BigDecimal expectAmount = borrowInfo.getAmount()
        .multiply(monthRate.multiply(new BigDecimal(borrowInfo.getPeriod())));

    Lend lend = Lend.builder().userId(borrowInfo.getUserId())
        .borrowInfoId(borrowInfo.getId())
        .lendNo(LendNoUtils.getLendNo())
        .title(borrowInfoApprovalVO.getTitle())
        .amount(borrowInfo.getAmount())
        .period(borrowInfo.getPeriod())
        .lendYearRate(lendYearRate)
        .serviceRate(serviceRate)
        .returnMethod(borrowInfo.getReturnMethod())
        .lowestAmount(new BigDecimal(100)) /*最低投资金额*/
        .investAmount(new BigDecimal(0)) /*已投资金额*/
        .investNum(0) /*已投资数量*/
        .publishDate(LocalDateTime.now())
        .lendStartDate(lendStartDate)
        .lendEndDate(lendEndDate)
        .lendInfo(borrowInfoApprovalVO.getLendInfo())
        .expectAmount(expectAmount)
        .realAmount(new BigDecimal(0))
        .status(LendStatusEnum.INVEST_RUN.getStatus())
        .checkAdminId(1L)
        .checkTime(LocalDateTime.now())
        .build();

    baseMapper.insert(lend);
  }

  @Override
  public List<Lend> selectList() {

    List<Lend> lendList = baseMapper.selectList(null);
    lendList.forEach(lend -> {
      String returnMethod = dictService
          .getNameByParentDictCodeAndValue("returnMethod", lend.getReturnMethod());

      String status = LendStatusEnum.getMsgByStatus(lend.getStatus());
      lend.getParam().put("returnMethod", returnMethod);
      lend.getParam().put("status", status);
    });

    return lendList;
  }
}
